package com.klb.service.catalog.impl;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.klb.dao.impl.ArticleDaoImpl;
import com.klb.dao.impl.BlockDaoImpl;
import com.klb.dao.impl.ReplyDaoImpl;
import com.klb.dao.impl.UserDaoImpl;
import com.klb.model.BbsArticle;
import com.klb.model.BbsBlock;
import com.klb.model.BbsReply;
import com.klb.model.BbsUser;
import com.klb.service.catalog.SingleServiceCatalog;
import com.klb.tools.Tools;

/**
 * Created by fujia on 2016/6/6.
 */
public class SingleServiceCatalogImpl implements SingleServiceCatalog {
    private ArticleDaoImpl articleDao;
    private BlockDaoImpl blockDao;
    private ReplyDaoImpl replyDao;
    private UserDaoImpl userDao;

    public void setArticleDao(ArticleDaoImpl articleDao) {
        this.articleDao = articleDao;
    }

    public void setBlockDao(BlockDaoImpl blockDao) {
        this.blockDao = blockDao;
    }

    public void setReplyDao(ReplyDaoImpl replyDao) {
        this.replyDao = replyDao;
    }

    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }


    @Override
    public BbsArticle getArticleById(int articleId) {

        if (!Tools.objectIsNullOrNot(articleId)){
            BbsArticle article = this.articleDao.getArticleById(articleId);
            if (!Tools.objectIsNullOrNot(article)){
                return article;
            }
        }

        return null;
    }

    @Override
    public String getBlockNameByArticle(BbsArticle article) {

        if (!Tools.objectIsNullOrNot(article)) {
            BbsBlock block = this.blockDao.getBlockById(article.getBlockId());
            if (!Tools.objectIsNullOrNot(block)) {
                return block.getTitle();
            }
        }

        return null;
    }

    @Override
    public List<HashMap<String, Object>> packReplyByArticleId(int articleId) {
        List<HashMap<String, Object>> data = new ArrayList<>();

        if (!Tools.objectIsNullOrNot(articleId)){
            List<BbsReply> replies = this.replyDao.getReplysByArticleId(articleId);

            if (!Tools.objectIsNullOrNot(replies)){
                for (BbsReply reply : replies){
                    BbsUser user = this.userDao.getUserById(reply.getUserId());

                    if (!Tools.objectIsNullOrNot(user) && !Tools.objectIsNullOrNot(reply)){
                        HashMap<String, Object> map = new HashMap<>();

                        map.put("replyId", reply.getReplyId());
                        map.put("articleId", reply.getArticleId());
                        map.put("userId", reply.getUserId());
                        map.put("text", reply.getText());
                        map.put("date", reply.getDateAdded());
                        map.put("time", reply.getTimeAdded());
                        map.put("username", user.getName());

                        data.add(map);
                    }
                }

                return data;
            }
        }

        return null;
    }

    @Override
    public boolean addReplyIsSuccess(int userId, int articleId, String text) {

        /**
         * 检验传入的userId和articleId不为空
         * 生成时间类
         * 打包成BbsReply类
         * 存入数据库
         * bbs_articles指定文章评论加一
         * */
        if (!Tools.objectIsNullOrNot(userId) && !Tools.objectIsNullOrNot(articleId)){
            java.util.Date nowDate = new java.util.Date();
            Date date = new Date(nowDate.getTime());
            Time time = new Time(nowDate.getTime());
            if (!Tools.objectIsNullOrNot(date) && !Tools.objectIsNullOrNot(time)){
                BbsReply reply = this.generateBbsReply(userId, articleId, text, date, time);
                if (this.replyDao.addQuery(reply)) {
                    if (this.articleDao.addCommentByArticleId(articleId))
                        return true;
                }
            }
        }

        return false;
    }

    /**
     * 构建BbsReply类
     * */
    private BbsReply generateBbsReply(int userId, int articleId, String text, Date dateAdded, Time timeAdded){
        BbsReply bbsReply = new BbsReply();

        bbsReply.setUserId(userId);
        bbsReply.setArticleId(articleId);
        bbsReply.setText(text);
        bbsReply.setDateAdded(dateAdded);
        bbsReply.setTimeAdded(timeAdded);

        return bbsReply;
    }
}
